﻿using System;
using System.IO;
using CommDll;
using System.Collections.Generic;
using System.Windows.Forms;

namespace HospitalMedicineTraceability
{
    /// <summary>
    /// 系统参数类
    /// </summary>
    public class AppConfig
    {
        //配置文件
        public static IniFile m_iniSetup = new IniFile(Path.GetFullPath("Config.ini"));
        // 01 系统设置类
        public static string m_strUserBefore = "";  //登录用户
        public static int m_nLoginFailedTimes = 3;  // 登录失败次数        
        // 02 系统使用类
        //消息提示是否正常
        public static Boolean m_bIsMessageOK = false;    
        // 日期提前量
        public static int m_nDateBeforeStockIn = 0; // 入库
        public static int m_nDateBeforeInvoice = 0; //发票
        public static int m_nDateBeforeInvoiceCheck = 0; //发票核对
        public static int m_nDateBeforeInvoiceSettle = 0; //发票结算

        public static bool m_bIsCollectHint = true;    // 采集异常是否提示
        public static bool m_bIsCollectSave = true;    // 采集正常是否保存
        // DataViewGrid显示信息
        public static Dictionary<string, string> m_dictFieldSql = new Dictionary<string, string>(); // Sql字段值
        public static Dictionary<string, string> m_dictFieldName = new Dictionary<string, string>();    // 字段值
        public static Dictionary<string, string> m_dictField0 = new Dictionary<string, string>();   // 显示字段默认值
        public static Dictionary<string, string> m_dictField = new Dictionary<string, string>();    // 显示字段设置值        
        public static Dictionary<string, string> m_dictFieldWidth0 = new Dictionary<string, string>();  // 显示字段宽度默认值
        public static Dictionary<string, string> m_dictFieldWidth = new Dictionary<string, string>();   // 显示字段宽度设置值

        public static string m_strPharmacyCode = "";    // 默认药房

        public static string m_strBillKind = "";    // 0:处方单号 1:身份证号 2:通用

        /// <summary>
        /// 读取配置数据
        /// </summary>
        public static void _getConfigInfo()
        {
            // 01 基础信息            
            m_strUserBefore = m_iniSetup._readValue("Common", "User", "").Trim();
            m_bIsMessageOK = FunctionComm._strToInt(m_iniSetup._readValue("Common", "IsMessageOK", "1").Trim(), 0) == 1;
            m_bIsCollectHint = FunctionComm._strToInt(m_iniSetup._readValue("Common", "IsCollectHint", "0").Trim(), 0) == 1;
            m_bIsCollectSave = FunctionComm._strToInt(m_iniSetup._readValue("Common", "IsCollectSave", "0").Trim(), 0) == 1;
            m_strPharmacyCode = m_iniSetup._readValue("Common", "PharmacyCode", "").Trim();

            m_nDateBeforeStockIn = FunctionComm._strToInt(m_iniSetup._readValue("Common", "DateBeforeStockIn", "3").Trim(), 0);
            m_nDateBeforeInvoice = FunctionComm._strToInt(m_iniSetup._readValue("Common", "DateBeforeInvoice", "3").Trim(), 0);
            m_nDateBeforeInvoiceCheck = FunctionComm._strToInt(m_iniSetup._readValue("Common", "DateBeforeInvoiceCheck", "3").Trim(), 0);
            m_nDateBeforeInvoiceSettle = FunctionComm._strToInt(m_iniSetup._readValue("Common", "DateBeforeInvoiceSettle", "3").Trim(), 0);
        }

        #region 自定义DataGridView显示
        /// <summary>
        /// 初始化字段显示数据项
        /// </summary>
        public static void _initDictField(string Kind)
        {
            m_dictFieldSql.Remove(Kind);
            m_dictFieldName.Remove(Kind);
            m_dictField0.Remove(Kind);
            m_dictField.Remove(Kind);
            m_dictFieldWidth0.Remove(Kind);
            m_dictFieldWidth.Remove(Kind);
        }

        /// <summary>
        /// 保存DataGridView字段显示信息
        /// </summary>
        public static void _saveDgvTitle(string strKind)
        {
            m_iniSetup._writeValue("Common", string.Format("{0}_Field", strKind), m_dictField[strKind]);
            m_iniSetup._writeValue("Common", string.Format("{0}_FieldWidth", strKind), m_dictFieldWidth[strKind]);
        }

        /// <summary>
        /// 保存dgv宽度数据
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="strKind"></param>
        /// <param name="bIsSelect">有选择框时特殊处理</param>
        public static void _saveDgvWidth(DataGridView dgv,string strKind,Boolean bIsSelect = false)
        {
            string strWidth = "";
            int nBegin = 0;
            bIsSelect = dgv.Columns.Contains("IsSelected");
            nBegin = bIsSelect ? 2 : 0;
            for (int i = nBegin; i < dgv.ColumnCount; i++)
            {
                strWidth += (strWidth.Equals("")?"":",") + (dgv.Columns[i].Visible ? dgv.Columns[i].Width.ToString():"0");
            }
            // 保存数据
            m_dictFieldWidth[strKind] = strWidth;
            m_iniSetup._writeValue("Common", string.Format("{0}_FieldWidth", strKind), m_dictFieldWidth[strKind]);
        }
        #endregion
    }
}